package com.yunhai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunhai.common.core.page.TableDataInfo;
import com.yunhai.common.utils.StringUtils;
import com.yunhai.system.domain.News;
import com.yunhai.system.mapper.NewsMapper;
import com.yunhai.system.service.NewsService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 资讯表(News)表服务实现类
 *
 * @author makejava
 * @since 2021-01-10 15:52:53
 */
@Service("newsService")
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public TableDataInfo getNewsByType(Map<String, Object> param) {
        String newsType = param.get("newsType") != null ? param.get("newsType").toString() : null;
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(newsType)) {
            queryWrapper.eq("news_type", param.get("newsType"));
        }
        queryWrapper.eq("language", MapUtils.getLong(param, "language"));
        Long page = MapUtils.getLong(param, "page", 1L);
        Long limit = MapUtils.getLong(param, "limit", 10L);
        queryWrapper.orderByDesc("push_time","create_time");
        IPage<News> pageResult = page(new Page<>(page, limit), queryWrapper);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(pageResult.getTotal());
        tableDataInfo.setRows(pageResult.getRecords());
        return tableDataInfo;
    }

    @Override
    public News getNewsById(String newsId) {
        return newsMapper.selectById(newsId);
    }

    @Override
    public TableDataInfo getNewsByKeyWord(String keyword, Long page, Long limit) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword);
        IPage<News> pageResult = page(new Page<>(page, limit), queryWrapper);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(pageResult.getTotal());
        tableDataInfo.setRows(pageResult.getRecords());
        return tableDataInfo;
    }

    /**
     * 后台列表查询
     *
     * @param news
     * @return
     */
    @Override
    public List<News> queryList(News news) {
        LambdaQueryWrapper<News> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(news.getNewsType())) {
            lqw.eq(News::getNewsType, news.getNewsType());
        }
        if (StringUtils.isNotBlank(news.getTitle())) {
            lqw.eq(News::getTitle, news.getTitle());
        }
        if (news.getLanguage() != null) {
            lqw.eq(News::getLanguage, news.getLanguage());
        }
        String beginTime = MapUtils.getString(news.getParams(), "beginTime");
        String endTime = MapUtils.getString(news.getParams(), "endTime");

        if (StringUtils.isNotBlank(beginTime)) {
            lqw.ge(News::getPushTime, beginTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            lqw.le(News::getPushTime, endTime);
        }

        lqw.orderByDesc(News::getPushTime,News::getCreateTime);
        return this.list(lqw);
    }
}